
#include "page_com.h"

//=============================================================
lv_obj_t* page_user_base_load(void) {
  static lv_style_t style;                        //创建style
  lv_style_init(&style);                          //初始化style
  lv_style_set_radius(&style, 42);                //设置样式的圆角
  lv_style_set_bg_opa(&style, LV_OPA_COVER);      //设置样式透明度
  lv_style_set_bg_color(&style, lv_palette_lighten(LV_PALETTE_RED, 1)); //设置主背景颜色
  lv_style_set_bg_grad_color(&style, lv_palette_main(LV_PALETTE_BLUE)); //设置下半部背景
  lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);                    //下半部颜色填充方向
  lv_style_set_bg_main_stop(&style, 160);         //主色停止,作为渐变线的分界线
  lv_style_set_bg_grad_stop(&style, 190);         //渐变色停止,渐变值必须大于主色值
  //------------------------------------------------------------------
  lv_obj_t* root = lv_obj_create(lv_scr_act());
  lv_obj_set_size(root, 280, 240);
  lv_obj_add_style(root, &style, 0);
  lv_obj_set_scrollbar_mode(root, LV_SCROLLBAR_MODE_OFF);
  lv_obj_center(root);
  lv_obj_add_flag(root, LV_OBJ_FLAG_HIDDEN);
  return root;
}

//=============================================================
static lv_obj_t * label;
static void set_label_value(void * lab, int32_t v) {
  if (v == 0) v = 9999;
  lv_label_set_text_fmt((lv_obj_t*)lab, "Value: %d", v);
}
//-------------------------------------------------------------
lv_obj_t* page_user_login_load(void) {
  lv_obj_t* root = page_user_base_load();
  label = lv_label_create(root);
  lv_label_set_text(label, "page_user_login");
  lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
  lv_obj_clear_flag(root, LV_OBJ_FLAG_HIDDEN);
  return root;
}
//-------------------------------------------------------------
void page_user_login_loop (void* p) {
  lv_anim_t a;
  lv_anim_init(&a);
  lv_anim_set_exec_cb(&a, set_label_value);
  lv_anim_set_values(&a, 10, 0);
  lv_anim_set_repeat_count(&a, 1);
  lv_anim_set_time(&a, 3000);
  lv_anim_set_var(&a, label);
  lv_anim_start(&a);
}

//------------------------------------------------------------
void page_user_login_event(int key) {
  if (key == 0) page_manager_switch(PAGE_Watch);
  if (key == 1) page_manager_switch(PAGE_Menun);
}

//============================================================
static void set_angle(void * img, int32_t v) {
  lv_img_set_angle((lv_obj_t*)img, v);
}
static void set_zoom(void * img, int32_t v) {
  lv_img_set_zoom((lv_obj_t*)img, v);
}
//------------------------------------------------------------
lv_obj_t* page_user_menun_load(void) {
  lv_obj_t* root = page_user_base_load();
  LV_IMG_DECLARE(jzc);
  lv_obj_t *img1 = lv_img_create(root);
  lv_img_set_src(img1, &jzc);
  lv_obj_align(img1, LV_ALIGN_CENTER, 50, 50);
  lv_img_set_pivot(img1, 0, 0);

  lv_anim_t a;
  lv_anim_init(&a);
  lv_anim_set_var(&a, img1);
  lv_anim_set_exec_cb(&a, set_angle);
  lv_anim_set_values(&a, 0, 3600);
  lv_anim_set_time(&a, 5000);
  lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
  lv_anim_start(&a);
  lv_anim_set_exec_cb(&a, set_zoom);
  lv_anim_set_values(&a, 128, 256);
  lv_anim_set_playback_time(&a, 3000);
  lv_anim_start(&a);
  return root;
}

//------------------------------------------------------------
void page_user_menun_event(int key) {
  if (key == 0) page_manager_switch(PAGE_Login);
  if (key == 1) page_manager_switch(PAGE_Watch);
}

//=============================================================
static lv_obj_t * meter;
static void set_meter_value(void * indic, int32_t v) {
  lv_meter_set_indicator_end_value(meter, (lv_meter_indicator_t*)indic, v);
}
//------------------------------------------------------------
lv_obj_t* page_user_watch_load(void) {
  lv_obj_t* root = page_user_base_load();
  meter = lv_meter_create(root);
  lv_obj_set_size(meter, 240, 240);
  lv_obj_center(meter);

  /*Create a scale for the minutes*/
  /*61 ticks in a 360 degrees range (the last and the first line overlaps)*/
  lv_meter_scale_t * scale_min = lv_meter_add_scale(meter);
  lv_meter_set_scale_ticks(meter, scale_min, 61, 1, 10, lv_palette_main(LV_PALETTE_GREY));
  lv_meter_set_scale_range(meter, scale_min, 0, 60, 360, 270);
  /*Create an other scale for the hours. It's only visual and contains only major ticks*/
  lv_meter_scale_t * scale_hour = lv_meter_add_scale(meter);
  lv_meter_set_scale_ticks(meter, scale_hour, 12, 0, 0, lv_palette_main(LV_PALETTE_GREY));
  lv_meter_set_scale_major_ticks(meter, scale_hour, 1, 2, 20, lv_color_black(), 10);
  lv_meter_set_scale_range(meter, scale_hour, 1, 12, 330, 300);

  /*Add a the hands from images*/
  LV_IMG_DECLARE(img_hand)
  lv_meter_indicator_t * indic_s = lv_meter_add_needle_img(meter, scale_min, &img_hand, 5, 5);
  lv_meter_indicator_t * indic_m = lv_meter_add_needle_img(meter, scale_min, &img_hand, 5, 5);
  lv_meter_indicator_t * indic_h = lv_meter_add_needle_img(meter, scale_min, &img_hand, 5, 5);

  /*Create an animation to set the value*/
  lv_anim_t a1;
  lv_anim_init(&a1);
  lv_anim_set_exec_cb(&a1, set_meter_value);
  lv_anim_set_values(&a1, 0, 60);
  lv_anim_set_repeat_count(&a1, LV_ANIM_REPEAT_INFINITE);
  lv_anim_set_time(&a1, 60000);
  lv_anim_set_var(&a1, indic_s);
  lv_anim_start(&a1);

  lv_anim_t a2;
  lv_anim_init(&a2);
  lv_anim_set_exec_cb(&a2, set_meter_value);
  lv_anim_set_values(&a2, 0, 60);
  lv_anim_set_repeat_count(&a2, LV_ANIM_REPEAT_INFINITE);
  lv_anim_set_time(&a2, 3600000);
  lv_anim_set_var(&a2, indic_m);
  lv_anim_start(&a2);

  lv_anim_t a3;
  lv_anim_init(&a3);
  lv_anim_set_exec_cb(&a3, set_meter_value);
  lv_anim_set_values(&a3, 0, 60);
  lv_anim_set_repeat_count(&a3, LV_ANIM_REPEAT_INFINITE);
  lv_anim_set_time(&a3, 43200000);
  lv_anim_set_var(&a3, indic_h);
  lv_anim_start(&a3);

  return root;
}

//------------------------------------------------------------
void page_user_watch_event(int key) {
  if (key == 0) page_manager_switch(PAGE_Menun);
  if (key == 1) page_manager_switch(PAGE_Login);
}
